package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.mapper.GoodsBrandMapper;
import com.fengmi.service.GoodsBrandService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class GoodsBrandServiceImpl implements GoodsBrandService {

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Override
    public PageResultVO<MallGoodsBrand> findPage(PageDTO<MallGoodsBrand> pageDTO) {
        //非空判断
        if (pageDTO==null){
            return new PageResultVO<>(false,"参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize<=0){
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper层查询
        List<MallGoodsBrand> brandList = goodsBrandMapper.findBrandByPage(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<MallGoodsBrand> pageInfo = new PageInfo<>(brandList);
        //创建返回对象
        PageResultVO<MallGoodsBrand> pageResultVO = new PageResultVO<>(true,"查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    /**
     * 添加品牌
     * @param mallGoodsBrand 品牌实体类
     * @return resultVO
     */
    @Override
    public ResultVO saveInsert(MallGoodsBrand mallGoodsBrand) {
        //非空判断
        if (mallGoodsBrand==null){
            return new ResultVO(false,"参数不合法");
        }
        //调用方法添加到数据库
        int i = goodsBrandMapper.insertBrand(mallGoodsBrand);
        //返回结果
        return new ResultVO(i==1?true:false,i==1?"添加品牌成功":"添加品牌失败");
    }

    /**
     * 通过id查询某一个品牌
     * @param id 品牌id
     */
    @Override
    public ResultVO findOneBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        //调用mapper层方法查询品牌
        MallGoodsBrand brandById = goodsBrandMapper.getOneBrandById(id);
        //返回
        if (brandById == null){
            return new ResultVO(false,"系统出小差了~~~");
        }

        return new ResultVO(true,"查询成功",brandById);
    }

    /**
     * 修改品牌展示状态，实现开关功能，可以反选
     * @param id 品牌id
     * @param status  品牌状态
     */
    @Override
    public ResultVO toggleBrandShowStatus(String id, Integer status) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        int i = goodsBrandMapper.updateBrandStatusById(id, status);

        return new ResultVO(i==1?true:false,i==1?"修改品牌展示状态成功":"修改品牌展示状态失败");
    }

    /**
     * 删除品牌
     * @param id 品牌id
     */
    @Override
    public ResultVO deleteBrandById(String id) {
        if (StringUtils.isEmpty(id)){
            return new ResultVO(false,"参数不合法");
        }
        int i = goodsBrandMapper.delBrandByBrandId(id);

        return new ResultVO(i==1?true:false,i==1?"删除品牌成功":"删除品牌失败");
    }

    /**
     * 修改品牌信息
     * @param mallGoodsBrand 传参对象
     */
    @Override
    public ResultVO updateBrandInfo(MallGoodsBrand mallGoodsBrand) {
        if (mallGoodsBrand == null){
            return new ResultVO(false,"参数不合法");
        }
        int i = goodsBrandMapper.updateBrandInfo(mallGoodsBrand);

        return new ResultVO(i==1?true:false,i==1?"修改品牌信息成功":"修改品牌信息失败");
    }

    /**
     * 批量删除
     * @param ids 品牌id数组
     */
    @Override
    public ResultVO delMoreThanOneBrand(Integer[] ids) {
        if (ids == null || ids.length<=0){
            return new ResultVO(false,"参数不合法");
        }
        int i = goodsBrandMapper.delMore(ids);
        return new ResultVO(i>0?true:false,i>0?"删除成功，共计删除"+i+"条记录":"删除失败");
    }

    /**
     * 查询所有品牌
     */
    @Override
    public ResultVO selectAllBrand() {

        List<MallGoodsBrand> brandList = goodsBrandMapper.findAllBrand();

        return new ResultVO(true,"success",brandList);
    }
}
